package com.kye.express.business.jd.goods_all_category;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.shop.JDGoodsCategoryBean;
import com.kye.model.bean.jd.shop.request.RequestJDGoodsCategories;
import com.kye.model.constant.Constants;
import com.kye.model.datasource.JDShopDataSource;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 京东兑换-全部分类 Presenter
 * <p>
 * Created by SkyYu on 2018/1/31
 */
class JDAllCategoryConstract {

    public interface JDAllCategoryView extends IView {

        void onSuccessLoadData(OpenApiResult<List<JDGoodsCategoryBean>> result);

        void onApiError(OpenApiResult<List<JDGoodsCategoryBean>> result, int code, String msg);

        void onOtherError(Throwable e);
    }

    static class JDAllCategoryPresenter extends BasePresenter<JDAllCategoryView> {

        private JDShopDataSource mJdShopDataSource;

        JDAllCategoryPresenter() {
            mJdShopDataSource = new JDShopDataSource();
        }

        void requestToJDGoodsCategories() {
            RequestJDGoodsCategories requestJDGoodsCategories = new RequestJDGoodsCategories();
            requestJDGoodsCategories.setKySign(Constants.JD_KYSIGN);
            requestJDGoodsCategories.setRecommendType(Constants.JD_RECOMMENDTYPE);
            mJdShopDataSource.getJDGoodsCategories(requestJDGoodsCategories)
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<List<JDGoodsCategoryBean>>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<JDGoodsCategoryBean>> result, List<JDGoodsCategoryBean> resultBean) {
                            getView().onSuccessLoadData(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<JDGoodsCategoryBean>> result, int code, String msg) {
                            getView().onApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(e);
                        }
                    });
        }
    }
}